﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using Maticsoft.DBUtility;
using System.Data;

namespace ElingOA6_Admin_DSJCJ.DAL
{
    public partial class ItemPurchaseApply
    {

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        /// <param name="itemPurchaseApplyID">主键编号</param>
        /// <returns>得到一个对象实体</returns>
        public ElingOA6_Admin_DSJCJ.Model.ItemPurchaseApply GetModel(decimal itemPurchaseApplyID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 * from dbo.v_ItemPurchaseApply_vEmplDept ");
            strSql.Append(" where FItemPurchaseApplyID=@FItemPurchaseApplyID");
            SqlParameter[] parameters = {
					new SqlParameter("@FItemPurchaseApplyID", SqlDbType.Decimal)
			};
            parameters[0].Value = itemPurchaseApplyID;

            ElingOA6_Admin_DSJCJ.Model.ItemPurchaseApply model = new ElingOA6_Admin_DSJCJ.Model.ItemPurchaseApply();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        /// <param name="row">表行</param>
        /// <returns>得到一个对象实体</returns>
        public ElingOA6_Admin_DSJCJ.Model.ItemPurchaseApply DataRowToModel(DataRow row)
        {
            ElingOA6_Admin_DSJCJ.Model.ItemPurchaseApply model = new ElingOA6_Admin_DSJCJ.Model.ItemPurchaseApply();
            if (row != null)
            {
                if (row["FItemPurchaseApplyID"] != null && row["FItemPurchaseApplyID"].ToString() != "")
                {
                    model.FItemPurchaseApplyID = decimal.Parse(row["FItemPurchaseApplyID"].ToString());
                }
                if (row["FApplyDeptNo"] != null)
                {
                    model.FApplyDeptNo = row["FApplyDeptNo"].ToString();
                }
                if (row["deptName"] != null)
                {
                    model.DeptName = row["deptName"].ToString();
                }
                if (row["FEntityNo"] != null)
                {
                    model.FEntityNo = row["FEntityNo"].ToString();
                }
                if (row["FApplyEmplNo"] != null)
                {
                    model.FApplyEmplNo = row["FApplyEmplNo"].ToString();
                }
                if (row["FEmpName"] != null)
                {
                    model.FEmpName = row["FEmpName"].ToString();
                }
                if (row["FApplyReason"] != null)
                {
                    model.FApplyReason = row["FApplyReason"].ToString();
                }
                if (row["FApplyDate"] != null && row["FApplyDate"].ToString() != "")
                {
                    model.FApplyDate = DateTime.Parse(row["FApplyDate"].ToString());
                }
                if (row["FApplyIP"] != null)
                {
                    model.FApplyIP = row["FApplyIP"].ToString();
                }
                if (row["FUpdDate"] != null && row["FUpdDate"].ToString() != "")
                {
                    model.FUpdDate = DateTime.Parse(row["FUpdDate"].ToString());
                }
                if (row["FUpdIP"] != null)
                {
                    model.FUpdIP = row["FUpdIP"].ToString();
                }
                if (row["FUpdEmplNo"] != null)
                {
                    model.FUpdEmplNo = row["FUpdEmplNo"].ToString();
                }
                if (row["FStatus"] != null)
                {
                    model.FStatus = row["FStatus"].ToString();
                }
                if (row["Cou"] != null)
                {
                    model.Cou = row["Cou"].ToString();
                }

            }
            return model;
        }

        /// <summary>
        /// 增加数据 事务模式
        /// </summary>
        /// <param name="model">ItemPurchaseApplyModel 对象</param>
        /// <param name="entryModelList">entryModelList集合</param>
        /// <returns>增加</returns>
        public decimal Add(Model.ItemPurchaseApply model, List<Model.ItemPurchaseApplyEntry> entryModelList)
        {
            StringBuilder strSql = new StringBuilder();
            CommandInfo cmds = new CommandInfo();
            List<CommandInfo> cmdList = new List<CommandInfo>();

            strSql.Append("insert into dbo.t_ItemPurchaseApply(");
            strSql.Append("FApplyDeptNo,FEntityNo,FApplyEmplNo,FApplyReason,FApplyDate,FApplyIP,FUpdDate,FUpdIP,FUpdEmplNo,FStatus)");
            strSql.Append(" values (");
            strSql.Append("@FApplyDeptNo,@FEntityNo,@FApplyEmplNo,@FApplyReason,@FApplyDate,@FApplyIP,@FUpdDate,@FUpdIP,@FUpdEmplNo,@FStatus)");
            strSql.Append(";select @returnID=IDENT_CURRENT('dbo.t_ItemPurchaseApply')");
            SqlParameter[] parameters = {
					new SqlParameter("@FApplyDeptNo", SqlDbType.NVarChar,12),
					new SqlParameter("@FEntityNo", SqlDbType.NVarChar,12),
					new SqlParameter("@FApplyEmplNo", SqlDbType.NVarChar,15),
					new SqlParameter("@FApplyReason", SqlDbType.NVarChar,-1),
					new SqlParameter("@FApplyDate", SqlDbType.DateTime),
					new SqlParameter("@FApplyIP", SqlDbType.NVarChar,15),
					new SqlParameter("@FUpdDate", SqlDbType.DateTime),
					new SqlParameter("@FUpdIP", SqlDbType.NVarChar,15),
					new SqlParameter("@FUpdEmplNo", SqlDbType.NVarChar,15),
					new SqlParameter("@FStatus", SqlDbType.NVarChar,1),
                    new SqlParameter("@returnID",SqlDbType.Decimal)};
            parameters[0].Value = model.FApplyDeptNo;
            parameters[1].Value = model.FEntityNo;
            parameters[2].Value = model.FApplyEmplNo;
            parameters[3].Value = model.FApplyReason;
            parameters[4].Value = model.FApplyDate;
            parameters[5].Value = model.FApplyIP;
            parameters[6].Value = model.FUpdDate;
            parameters[7].Value = model.FUpdIP;
            parameters[8].Value = model.FUpdEmplNo;
            parameters[9].Value = model.FStatus;
            parameters[10].Direction = ParameterDirection.Output;

            cmds.CommandText = strSql.ToString();
            cmds.Parameters = parameters;
            cmdList.Add(cmds);

            if (entryModelList.Count > 0)
            {
                strSql = new StringBuilder();                
                strSql.Append("insert into dbo.t_ItemPurchaseApplyEntry(");
                strSql.Append("FItemPurchaseApplyID,FItemName,FItemSpec,FQty,FPrice,FAmount,FRequestDate,FNote)");
                strSql.Append(" values (");
                strSql.Append("@FItemPurchaseApplyID,@FItemName,@FItemSpec,@FQty,@FPrice,@FAmount,@FRequestDate,@FNote)");
                string strSqls = strSql.ToString();
                foreach (Model.ItemPurchaseApplyEntry entryModel in entryModelList)
                {
                    cmds = new CommandInfo();
                    SqlParameter[] parameters1 = {
					new SqlParameter("@FItemPurchaseApplyID", SqlDbType.Decimal),
					new SqlParameter("@FItemName", SqlDbType.NVarChar,30),
					new SqlParameter("@FItemSpec", SqlDbType.NVarChar,30),
					new SqlParameter("@FQty", SqlDbType.Decimal,13),
					new SqlParameter("@FPrice", SqlDbType.Decimal,13),
					new SqlParameter("@FAmount", SqlDbType.Decimal,13),
					new SqlParameter("@FRequestDate", SqlDbType.DateTime),
					new SqlParameter("@FNote", SqlDbType.NVarChar,200)};
                    parameters1[0].Direction = ParameterDirection.InputOutput;
                    parameters1[1].Value = entryModel.FItemName;
                    parameters1[2].Value = entryModel.FItemSpec;
                    parameters1[3].Value = entryModel.FQty;
                    parameters1[4].Value = entryModel.FPrice;
                    parameters1[5].Value = entryModel.FAmount;
                    parameters1[6].Value = entryModel.FRequestDate;
                    parameters1[7].Value = entryModel.FNote;

                    cmds.CommandText = strSqls;
                    cmds.Parameters = parameters1;
                    cmdList.Add(cmds);

                }
            }



            DbHelperSQL.ExecuteSqlTranWithString(cmdList);
            return decimal.Parse(parameters[10].Value.ToString());
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        /// <param name="model">ItemPurchaseApplyModel对象</param>
        /// <param name="entryModelList">entryModelList集合</param>
        public void Update(ElingOA6_Admin_DSJCJ.Model.ItemPurchaseApply model, List<Model.ItemPurchaseApplyEntry> entryModelList)
        {
            StringBuilder strSql = new StringBuilder();
            CommandInfo cmds = new CommandInfo();
            List<CommandInfo> cmdList = new List<CommandInfo>();

            strSql.Append("update dbo.t_ItemPurchaseApply set ");
            strSql.Append("FApplyReason=@FApplyReason,");
            strSql.Append("FUpdDate=@FUpdDate,");
            strSql.Append("FUpdIP=@FUpdIP,");
            strSql.Append("FUpdEmplNo=@FUpdEmplNo");
            strSql.Append(" where FItemPurchaseApplyID=@FItemPurchaseApplyID");
            SqlParameter[] parameters = {
					new SqlParameter("@FApplyReason", SqlDbType.NVarChar,-1),
					new SqlParameter("@FUpdDate", SqlDbType.DateTime),
					new SqlParameter("@FUpdIP", SqlDbType.NVarChar,15),
					new SqlParameter("@FUpdEmplNo", SqlDbType.NVarChar,15),
					new SqlParameter("@FItemPurchaseApplyID", SqlDbType.Decimal,13)};
            parameters[0].Value = model.FApplyReason;
            parameters[1].Value = model.FUpdDate;
            parameters[2].Value = model.FUpdIP;
            parameters[3].Value = model.FUpdEmplNo;
            parameters[4].Value = model.FItemPurchaseApplyID;

            cmds.CommandText = strSql.ToString();
            cmds.Parameters = parameters;
            cmdList.Add(cmds);


            //删除
            strSql = new StringBuilder();
            cmds = new CommandInfo();
            strSql.Append("delete from dbo.t_ItemPurchaseApplyEntry where FItemPurchaseApplyID=@FItemPurchaseApplyID");
            SqlParameter[] parameters0 = { 
                    new SqlParameter("@FItemPurchaseApplyID", SqlDbType.Decimal) };
            parameters0[0].Value = model.FItemPurchaseApplyID;

            cmds.CommandText = strSql.ToString();
            cmds.Parameters = parameters0;
            cmdList.Add(cmds);


            //保存
            if (entryModelList.Count > 0)
            {
                strSql = new StringBuilder();
                strSql.Append("insert into dbo.t_ItemPurchaseApplyEntry(");
                strSql.Append("FItemPurchaseApplyID,FItemName,FItemSpec,FQty,FPrice,FAmount,FRequestDate,FNote)");
                strSql.Append(" values (");
                strSql.Append("@FItemPurchaseApplyID,@FItemName,@FItemSpec,@FQty,@FPrice,@FAmount,@FRequestDate,@FNote)");
                string strSqls = strSql.ToString();
                foreach (Model.ItemPurchaseApplyEntry entryModel in entryModelList)
                {
                    cmds = new CommandInfo();
                    SqlParameter[] parameters1 = {
					new SqlParameter("@FItemPurchaseApplyID", SqlDbType.Decimal),
					new SqlParameter("@FItemName", SqlDbType.NVarChar,30),
					new SqlParameter("@FItemSpec", SqlDbType.NVarChar,30),
					new SqlParameter("@FQty", SqlDbType.Decimal,13),
					new SqlParameter("@FPrice", SqlDbType.Decimal,13),
					new SqlParameter("@FAmount", SqlDbType.Decimal,13),
					new SqlParameter("@FRequestDate", SqlDbType.DateTime),
					new SqlParameter("@FNote", SqlDbType.NVarChar,200)};
                    parameters1[0].Value = model.FItemPurchaseApplyID;
                    parameters1[1].Value = entryModel.FItemName;
                    parameters1[2].Value = entryModel.FItemSpec;
                    parameters1[3].Value = entryModel.FQty;
                    parameters1[4].Value = entryModel.FPrice;
                    parameters1[5].Value = entryModel.FAmount;
                    parameters1[6].Value = entryModel.FRequestDate;
                    parameters1[7].Value = entryModel.FNote;

                    cmds.CommandText = strSqls;
                    cmds.Parameters = parameters1;
                    cmdList.Add(cmds);

                }
            }

            DbHelperSQL.ExecuteSqlTranWithString(cmdList);
        }


        /// <summary>
        /// 删除一条数据
        /// </summary>
        /// <param name="itemPurchaseApplyID">主键ID</param>
        public void Delete(decimal itemPurchaseApplyID)
        {

            StringBuilder strSql = new StringBuilder();
            CommandInfo cmds = new CommandInfo();
            List<CommandInfo> cmdList = new List<CommandInfo>();

            strSql.Append("delete from dbo.t_ItemPurchaseApply ");
            strSql.Append(" where FItemPurchaseApplyID=@FItemPurchaseApplyID");
            SqlParameter[] parameters = {
					new SqlParameter("@FItemPurchaseApplyID", SqlDbType.Decimal)
			};
            parameters[0].Value = itemPurchaseApplyID;

            cmds.CommandText = strSql.ToString();
            cmds.Parameters = parameters;
            cmdList.Add(cmds);


            ///删除具体明细
            strSql = new StringBuilder();
            cmds = new CommandInfo();

            strSql.Append("delete from dbo.t_ItemPurchaseApplyEntry ");
            strSql.Append(" where FItemPurchaseApplyID=@FItemPurchaseApplyID");
            SqlParameter[] parameters1 = {
					new SqlParameter("@FItemPurchaseApplyID", SqlDbType.Decimal)
			};
            parameters1[0].Value = itemPurchaseApplyID;

            cmds.CommandText = strSql.ToString();
            cmds.Parameters = parameters;
            cmdList.Add(cmds);

            DbHelperSQL.ExecuteSqlTranWithString(cmdList);
        }


        /// <summary>
        /// 批量删除 事务模式
        /// </summary>
        /// <param name="itemPurchaseApplyIDArray">ID数组</param>
        public void ManyDelete(string[] itemPurchaseApplyIDArray)
        {
            StringBuilder strSql = new StringBuilder();
            CommandInfo cmds = new CommandInfo();
            List<CommandInfo> cmdList = new List<CommandInfo>();

            if (itemPurchaseApplyIDArray.Length > 0)
            {
                //删除广告数据
                strSql = new StringBuilder();
                strSql.Append("delete from dbo.t_ItemPurchaseApply ");
                strSql.Append(" where FItemPurchaseApplyID=@FItemPurchaseApplyID ");

                string strSqlApply = strSql.ToString();


                strSql = new StringBuilder();
                strSql.Append("delete from dbo.t_ItemPurchaseApplyEntry ");
                strSql.Append(" where FItemPurchaseApplyID=@FItemPurchaseApplyID ");

                string strSqlEntry = strSql.ToString();

                foreach (string itemPurchaseApplyID in itemPurchaseApplyIDArray)
                {
                    cmds = new CommandInfo();
                    SqlParameter[] parameters = {
					    new SqlParameter("@FItemPurchaseApplyID", SqlDbType.NVarChar,15)};

                    parameters[0].Value = itemPurchaseApplyID;

                    cmds.CommandText = strSqlApply;
                    cmds.Parameters = parameters;
                    cmdList.Add(cmds);

                    cmds = new CommandInfo();

                    cmds.CommandText = strSqlEntry;
                    cmds.Parameters = parameters;
                    cmdList.Add(cmds);


                }

            }

            DbHelperSQL.ExecuteSqlTranWithString(cmdList);

        }

        /// <summary>
        /// 审核
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public bool Check(Model.ItemPurchaseApply model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update dbo.t_ItemPurchaseApply set ");
            strSql.Append("FAuditDate=@FAuditDate,");
            strSql.Append("FAuditEmplNo=@FAuditEmplNo,");
            strSql.Append("FAuditIP=@FAuditIP,");
            strSql.Append("FStatus=@FStatus");
            strSql.Append(" where FItemPurchaseApplyID=@FItemPurchaseApplyID");
            SqlParameter[] parameters = {
					new SqlParameter("@FStatus", SqlDbType.NVarChar,1),
                    new SqlParameter("@FAuditDate", SqlDbType.DateTime),
                    new SqlParameter("@FAuditEmplNo", SqlDbType.NVarChar,15),
                    new SqlParameter("@FAuditIP", SqlDbType.NVarChar,15),
					new SqlParameter("@FItemPurchaseApplyID", SqlDbType.Decimal,13)};
            parameters[0].Value = model.FStatus;
            parameters[1].Value = model.FAuditDate;
            parameters[2].Value = model.FAuditEmplNo;
            parameters[3].Value = model.FAuditIP;
            parameters[4].Value = model.FItemPurchaseApplyID;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

    }
}
